package cn.walls1717.ggkt.vod.controller;

import cn.walls1717.ggkt.result.Result;
import cn.walls1717.ggkt.vod.service.VodService;
import cn.walls1717.ggkt.vod.util.ConstantPropertiesUtil;
import cn.walls1717.ggkt.vod.util.Signature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * 腾讯云视频相关接口
 *
 * @author walls1717
 * @since 2022/7/16
 **/
@Api(tags = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    @ApiOperation("上传视频接口")
    @PostMapping("upload")
    public Result<?> upload() {
        String fileId = vodService.uploadVideo();
        return Result.success(fileId);
    }

    @ApiOperation("删除腾讯云视频")
    @DeleteMapping("remove/{fileId}")
    public Result<?> deleteVideo(@PathVariable String fileId) {
        vodService.deleteVideo(fileId);
        return Result.success();
    }

    @ApiOperation("客户端上传视频签名")
    @GetMapping("sign")
    public Result<?> sign() {
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtil.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        // 签名有效期：2天
        sign.setSignValidDuration(3600 * 24 * 2);
        try {
            String signature = sign.getUploadSignature();
            log.info("signature : {}",signature);
            return Result.success(signature);
        } catch (Exception e) {
            log.info("获取签名失败");
            e.printStackTrace();
            return Result.error(null);
        }
    }
}
